<template>
	<u-tabbar :value="checkVal" ref="RefTabber" :fixed="true" :placeholder="true" :safeAreaInsetBottom="true">
		<u-tabbar-item v-for="(route,index) of tabbarRoutes" :key="index" :text="route.name" @click="change"></u-tabbar-item>
	</u-tabbar>
</template>

<script>
	import {
		mapState
	} from "vuex"
	import {
		RouteTypeEnum
	} from "@/enums/route"
	const valStorageKey = "wakin_tabbar_val"
	export default {
		name: "WakinTabbar",
		data() {
			return {
				checkVal: uni.getStorageSync(valStorageKey) ?? null,
				refTabber: null
			}
		},
		computed: {
			...mapState('user', {
				tabbarRoutes: state => state.tabbarRoutes,
				perfect: state => state.perfect
			})
		},
		mounted() {
			// if (!this.refTabber) this.init()
			// else this.refTabber = this.$refs.RefTabber
			// this.init();
		},
		methods: {
			init() {
				console.log("this.perfect: ", this.perfect);
				const val = uni.getStorageSync(valStorageKey);
				if (uni.$u.test.empty(val)) {
					let index = 0;
					if (!this.perfect) {
						index = this.routes.findIndex(f => f.key === "my");
						this.checkVal = index > 0 ? index : 0
					} else this.checkVal = 0;
					this.$jump({
						to: this.routes[index].path ?? "",
						type: "reLaunch",
						login: false
					})
					uni.setStorageSync(valStorageKey, this.checkVal)
				} else {
					this.change(val);
				}

			},
			change(e) {
				this.checkVal = e;
				uni.setStorageSync(valStorageKey, this.checkVal);
				this.$jump({
					to: this.routes[this.checkVal].path ?? "",
					mode: "reLaunch",
					login: false
				})
				console.log("change: ", e, this.routes[this.checkVal].path);
			}
		},
		destroyed() {
			console.log("销毁wakin-tabbar");
			uni.removeStorageSync(valStorageKey)
		},
	}
</script>

<style lang="less" scoped>
</style>
